﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class miscellaneousCharges : Form
    {
        selection parent;
        MiscChargeController miscCharge;

        public miscellaneousCharges(selection parent)
        {
            this.parent = parent;
            miscCharge = new MiscChargeController();
            InitializeComponent();
            initializeContent();
        }

        private void back_Click(object sender, EventArgs e)
        {
            this.Hide();
            parent.Show();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            DialogResult result = MessageBox.Show("Are you sure you want to exit the program?","Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.No)
                e.Cancel = true;
            else
                Environment.Exit(1);
        }

        private void addOrEditMiscCharges_Click(object sender, EventArgs e)
        {
            addOrEditMiscellaneousCharges addOrEditMiscCharges = new addOrEditMiscellaneousCharges(this);
            addOrEditMiscCharges.ShowDialog();
        }

        private void deleteMiscCharges_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to delete the current miscellaneous charges?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.No)
            {
            }
            else
            {
                MiscChargePeriod temp = new MiscChargePeriod();
                DateTime tempDate = DateTime.Parse(getCurrentDate());
                temp.setStartDate(tempDate.ToString("yyyy-MM-dd"));
                temp.setMiscCharge(0);
                bool success = miscCharge.editMiscCharge(temp);
                if (success)
                {
                    initializeContent();
                }
            }
        }

        public String getCurrentDate()
        {
            return miscCharge.getCurrentDate();
        }

        public String getCurrentEnd()
        {
            return miscCharge.getCurrentEnd();
        }

        public decimal getAmount()
        {
            return miscCharge.getCurrentChargeAmount();
        }

        public void initializeContent()
        {
            BindingSource bindingSource = new BindingSource();
            bindingSource.DataSource = miscCharge.getData();
            miscellaneousChargesTable.DataSource = bindingSource;
        }
    }
}
